import axios from 'axios'
import  Vue from 'vue'
import { Notify } from 'vant';
const envHttpRequestUrl = process.env.VUE_APP_API_BASE_URL;
var base = envHttpRequestUrl.endsWith("/") ? envHttpRequestUrl.substr(0,envHttpRequestUrl.length - 1) : envHttpRequestUrl;
const IS_DEVELOPMENT = ["development"].includes(process.env.NODE_ENV);
if(IS_DEVELOPMENT){
  base = '/shhhxcch'
}

import {ACCESS_TOKEN} from "../common/common";
// 超时设定
axios.defaults.timeout = 60000

axios.interceptors.request.use(config => {
  config.headers.crossDomain=true //允许跨域
  config.headers.withCredentials=true //需要携带cookie
  return config
}, err => {
  return Promise.resolve(err)
})
// http response 拦截器
axios.interceptors.response.use(response => {
  const data = response.data
  return data
}, (err) => {
  Notify({ type: 'danger', message: '请求错误' });
  // 返回状态码不为200时候的错误处理
  return Promise.resolve(err)
})
// get请求
export const getRequest = (url, params) => {

  const accessToken = Vue.ls.get(ACCESS_TOKEN)
  return axios({
    method: 'get',
    url: `${base}${url}`,
    params: params,
    headers: {
      token: accessToken
    }
  })
}
// post请求
export const postRequest = (url, params) => {
  const accessToken = Vue.ls.get(ACCESS_TOKEN)
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: JSON.stringify(params),
    headers: {
      'Content-Type': 'application/json',
      token: accessToken
    }
  })
}
// 请求
export const importRequest = (url, params) => {
  const accessToken = Vue.ls.get(ACCESS_TOKEN)
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    headers: {
      token: accessToken
    }
  })
}

export const uploadFileRequest = (url, params) => {
  const accessToken = Vue.ls.get(ACCESS_TOKEN)
  return axios({
    method: 'post',
    url: `${base}${url}`,
    params: params,
    headers: {
      token: accessToken
    }
  })
}
export const getNoAuthRequest = (url, params) => {
  return axios({
    method: 'get',
    url: `${base}${url}`,
    params: params
  })
}
